package com.hdw.common.base.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdw.common.base.api.BaseMsgService;
import com.hdw.common.base.mapper.BaseMsgMapper;
import com.hdw.common.constant.CommonConstant;
import com.hdw.common.vo.BaseMsg;
import com.hdw.common.vo.PaginationVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * baseMsg服务实现类
 * Created by liujunlei on 2022/01/10.
 */
@Service
public class BaseMsgServiceImpl implements BaseMsgService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseMsgServiceImpl.class);

    @Autowired
    private BaseMsgMapper baseMsgMapper;

    public int insert(String cd,String msg,String des) {
        BaseMsg baseMsg = getBaseMsgByCd(cd);
        return baseMsgMapper.insert(baseMsg);
    }

    public int getCount(BaseMsg record) {
        return baseMsgMapper.getCount(record);
    }

    private BaseMsg getBaseMsgByCd(String cd) {
        BaseMsg baseMsg = new BaseMsg();
        baseMsg.setCd(cd);
        baseMsg.setDtSysCre(new Date());
        baseMsg.setFgHand(false);
        switch (cd) {
            case "A01":
                baseMsg.setNa("系统异常");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
            case "B01":
                baseMsg.setNa("元数据创建异常");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
            case "C01":
                baseMsg.setNa("定时调度执行失败");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
            case "C02":
                baseMsg.setNa("JOB执行失败");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
            case "D01":
                baseMsg.setNa("新增维度");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
            case "E01":
                baseMsg.setNa("数据条数核对不上");
                baseMsg.setEuTp(CommonConstant.MSG_TYPE_ERROR);
                break;
        }
        return baseMsg;
    }

    public int updateByIdSelective(BaseMsg baseMsg)  {

        return baseMsgMapper.updateByPrimaryKeySelective(baseMsg);
    }

    public PaginationVO<BaseMsg> findPageByModel(BaseMsg baseMsg, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseMsg> quesList = baseMsgMapper.findByModel(baseMsg);
        PageInfo<BaseMsg> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseMsg> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }


}
